﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VehicleManagement.Codes;
using VehicleManagement.Models;
using System.Text;

namespace VehicleManagement.ControllersServices
{
    public class ApplicantService
    {
        public IList<Applicant> GetAllApplicants()
        {
            return SqlMapperManager.Instance.Get().QueryForList<Applicant>("SELECT_ALL_Applicant", null);
        }

        public object InsertApplicant(Applicant applicant)
        {
            object result = SqlMapperManager.Instance.Get().Insert("INSERT_Applicant", applicant);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(applicant.Name);
            sb.AppendLine(applicant.LoginName);
            sb.AppendLine(applicant.Position);
            sb.AppendLine(applicant.Remark);
            Utility.SendEmail(applicant.Email,"驭舆企业汽车管理系统——申请人",sb.ToString());
            return result;
        }

        public int UpdateApplicant(Applicant applicant)
        {
            return SqlMapperManager.Instance.Get().Update("UPDATE_Applicant", applicant);
        }

        public int DeleteApplicant(int id)
        {
            return SqlMapperManager.Instance.Get().Update("DELETE_Applicant", id);
        }

        public IList<Applicant> SearchAllApplicants(string name, string position, string sex,string company)
        {
            return SqlMapperManager.Instance.Get().QueryForList<Applicant>("SEARCH_APPLICANT", new
            {
                Name = name,
                Position = position,
                Sex = sex,
                Company = company
            });
        }

        public IList<Applicant> SearchAllApplicants(string name, string position, string sex, int skip, int max, string company)
        {
            return SqlMapperManager.Instance.Get().QueryForList<Applicant>("SEARCH_APPLICANT", new
            {
                Name = name,
                Position = position,
                Sex = sex,
                Company = company
            },skip,max);
        }

        public int GetApplicantCount(string name, string position, string sex,string company)
        {
            return SearchAllApplicants(name, position, sex, company).Count;
        }
    }
}